<!DOCTYPE html>
<html>
<head>
    <title>课程名称</title>
    {% include 'admin/common/header.html' %}</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">课程名</label>
                <div class="layui-input-inline">
                    <input type="text" name="name" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="lesson-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="lesson-table" lay-filter="lesson-table"></table>
    </div>
</div>
</body>


<script type="text/html" id="lesson-bar">
    {% if authorize("admin:lesson:remove") %}
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="emotion">
            识别情绪
        </button>
    {% endif %}
</script>

<script type="text/html" id="lesson-enable">
    <input type="checkbox" name="enable" value="{{ "{{d.id}}" }}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="lesson-enable" {{ "{{# if(d.enable==1){ }} checked {{# } }}" }}>
</script>
{% include 'admin/common/footer.html' %}
<script>
    layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
        let table = layui.table
        let form = layui.form
        let $ = layui.jquery
        let popup = layui.popup

        let MODULE_PATH = '/admin/lesson/'

        let cols = [
            [
                { title: '课程名称', field: 'name', align: 'center' },
                { title: '简介', field: 'note', align: 'center' },
                { title: '操作', toolbar: '#lesson-bar', align: 'center', width: 195 }
            ]
        ]

        table.render({
            elem: '#lesson-table',
            url: MODULE_PATH + 'data',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#lesson-toolbar',
            defaultToolbar: [{
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        })


        form.on('submit(lesson-query)', function (data) {
            table.reload('lesson-table', { where: data.field })
            return false
        })


        table.on('tool(lesson-table)', function (obj) {
            if (obj.event === 'emotion') {
                window.emotion(obj)
            }
        })


        window.emotion = function (obj) {
            layer.confirm('确定要识别情绪吗？', { icon: 3, title: '提示' }, function (index) {
                $.ajax({
                    url: MODULE_PATH + 'emotion/' + obj.data['id'],
                    dataType: 'json',
                    type: 'get',
                    success: function (result) {
                        layer.closeAll();
                        let html = "<div style = 'padding: 40px'>"
                        result.forEach((item,index)=>{
                            html = html + "<div style = 'padding: 40px'>"
                                html = html + item
                            html = html + "</div>"
                        })
                        "</div>";


                        layer.open({
                            type: 1,
                            title: false,
                            closeBtn: 0,
                            shadeClose: true,
                            content: html
                        });


                    }
                })
            })
        }
    })
</script>
</html>